package address.mapper;

import common.model.user.Address;
import org.apache.ibatis.annotations.*;

import java.util.List;

@Mapper
public interface AddressMapper {
    @Select("SELECT * FROM address WHERE user_id=#{uid}")
    List<Address> list(Long uid);

    @Select("SELECT * FROM address WHERE user_id=#{uid} AND is_default=1")
    Address getDefault(Long uid);

    @Insert("INSERT INTO address(user_id, recipient_name, telephone, province, city, district, " +
            "address_detail, postal_code, is_default) " +
            "VALUES(#{uid}, #{recipientName}, #{telephone}, #{province}, #{city}, #{district}, " +
            "#{addressDetail}, #{postalCode}, #{isDefault})")
    void create(Address address);

    @Update("UPDATE address SET recipient_name=#{recipientName}, telephone=#{telephone}, " +
            "province=#{province}, city=#{city}, district=#{district}, " +
            "address_detail=#{addressDetail}, postal_code=#{postalCode}, " +
            "is_default=#{isDefault} WHERE id=#{id}")
    void update(Address address);

    @Delete("DELETE FROM address WHERE id=#{id}")
    void delete(Long id);
//设置默认地址
    @Update("UPDATE address SET is_default=1 WHERE id=#{id}")
    void setDefault(Long id);
//清空默认地址
    @Update("UPDATE address SET is_default = 0 WHERE user_id=#{uid}")
    void clearDefault(Long uid);
}
